<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once("./_inc/crulFunction.php");

$errMsg = "";
$module = sxREQ('mod');
$name = sxREQ('name');
$alias = sxREQ('alias');

if($module=='' || $name=='') {
	$url = $_SESSION[USEROBJ]->getHistory(-1, true).
		"&msg=".urlencode(_t("missing name '$module/$name'"));
	serverRedir($url);
}

if(sxREQ("reqtype") == "save") {
	$errMsg .= crulVarIns();
}
$sSQL = "SELECT cra.*  "
	." FROM com_rule_alias cra " 
	." WHERE cra.module='".$module."' "
	." AND cra.name='".$name."' "
	." AND cra.com_alias='".$alias."'";
$objSDAO->query($sSQL,'crulAlAtt');
if(!$rowA = $objSDAO->fetch('crulAlAtt')) {
	$url = $_SESSION[USEROBJ]->getHistory(-1, true).
		"&msg=".urlencode(_t("missing alias '$module/$name/$alias'"));
	serverRedir($url);
}
sxRDef('description_alias',$rowA['description']);
sxRDef('read_only_alias',$rowA['read_only']);
sxRDef('note_alias',$rowA['note']);
sxRDef('read_only',$rowA['read_only']);

$aOpz = array("kMenu"=>'pro.cru', "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
function objFrmSubm(rt){
	document.objFrm.reqtype.value = rt;
	document.objFrm.submit();
}
</script>
<?php
echo(bodyHTML($aOpz));

echo getCrulHeader($module,$name);

$aCmd = array("save"=>array("pag"=>"javascript:objFrmSubm('save')",
										"title"=>_t("save"),
										"text"=>_t("save")
										),
					"back"=>array()
					);
$sCmd = commandsHTML($aCmd);	
echo $sCmd;

echo getHtmlForm('objFrm');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('reqtype','...');
echo getHtmlHidden('mod');
echo getHtmlHidden('name');
echo getHtmlHidden('alias');

echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" width=\"20%\" colspan='4'>Variable</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\" width=\"15%\">Alias</td>" .
			"<td class=\"text01\" width=\"15%\">".$alias."</td>" .
			"<td class=\"labl01\" width=\"15%\">Read-only</td>" .
			"<td class=\"text01\" width=\"55%\">".
			getHtmlVisDecode("read_only_alias",array('N'=>'No','Y'=>'Yes'))."</td>" .
		"</tr>\n");
if (sxRTrim('description_alias')!='') {
	echo("<tr>" .
			"<td class=\"labl01\">Description</td>" .
			"<td class=\"text01\" colspan='3'>".
			getHtmlVisText('description_alias')."</td>" .
		"</tr>\n");
}
if (sxRTrim('note_alias')!='') {
	echo("<tr>" .
			"<td class=\"labl01\">Note</td>" .
			"<td class=\"text01\" colspan='3'>".
			getHtmlVisNote("note_alias")."</td>" .
		"</tr>\n");
}
			echo("<tr>" .
			"<td class=\"labl01\">Name*</td>" .
			"<td class=\"text01\" colspan='3'>".getHtmlText('variable',80,80)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Description</td>" .
			"<td class=\"text01\" colspan='3'>".getHtmlText('description',80,80)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Read-only</td>" .
			"<td class=\"text01\" colspan='3'>");
if (sxREQ('read_only_alias')=='Y') echo 'Yes'.getHtmlHidden('read_only','Y');
else echo getHtmlVisDecode('read_only', array('N'=>'No','Y'=>'Yes'));
echo ("</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Note</td>" .
			"<td class=\"text01\" colspan='3'>".getHtmlTextArea('note',80,3)."</td>" .
		"</tr>\n");
echo("</table>");
echo("</form>");
echo $sCmd;
echo(footerHTML());
exit();

function crulVarIns(){
 	global $objSDAO;
	$errMsg = "";
	$_REQUEST['variable'] = strtolower(sxREQ('variable'));
	if(preg_match("[^a-z0-9_]",sxREQ('variable')))
			$errMsg .= "Name: letter, numbers and _ only<br>";
	if(sxRTrim('variable')=='')
		$errMsg .= " missing name<br>";	
	//existence
	$sSQL = "SELECT * FROM com_rule_variable " 
			." WHERE module='".sxRAS("mod")."' " 
			." AND name='".sxRAS("name")."'" 
			." AND com_alias='".sxRAS("alias")."'"
			." AND variable = '".sxRAS('variable')."'";
	$objSDAO->query($sSQL);
	if($row = $objSDAO->fetch())
		$errMsg .= "existing variable<br>";
		
	if($errMsg != "") return $errMsg;
			
	$sSQL = "INSERT INTO com_rule_variable " .
				" (module,name,com_alias,variable,description,read_only,note)" .
				" VALUES('".sxRAS('mod')."','".sxRAS('name')."','".sxRAS('alias')."',
				'".sxRAS('variable')."','".sxRAS('description')."','".sxRAS('read_only')."'"
				.",'".sxRAS('note')."') " ;
		
	$objSDAO->exec($sSQL);
	$url = $_SESSION[USEROBJ]->getHistory(0, true);
	serverRedir($url);
	return 'redir err';
}
